package gewu.suleju.controller;

import gewu.suleju.model.pojo.HouseInfor;
import gewu.suleju.model.pojo.HousePic;
import gewu.suleju.model.pojo.HouserBrowsingHistory;
import gewu.suleju.model.vo.Result;
import gewu.suleju.sevice.imp.HouseInforServiceImp;
import gewu.suleju.sevice.imp.HousePicServiceImp;
import gewu.suleju.sevice.imp.IHouseBrowHistoryServiceImp;
import gewu.suleju.util.JWTUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("myselfHouseHistory")
public class MysellfHouseHistoryController {
    @Autowired
    private IHouseBrowHistoryServiceImp iHouseBrowHistoryServiceImp;
    @Autowired
    private HouseInforServiceImp houseInforServiceImp;

    @Autowired
    private HousePicServiceImp housePicServiceImp;
    @ResponseBody
    @GetMapping("findHouseHistoryByUserId")
    public Result findHouseHistoryByUserId(HttpServletRequest request, HttpServletResponse response) {

        String token = request.getHeader("token");
        Claims claims = JWTUtil.checkJWT(token);
        Integer userid=(Integer)claims.get("id");
        List<HouseInfor> houseInforList =new ArrayList<>();
        List<HouserBrowsingHistory> houserBrowsingHistoryList=iHouseBrowHistoryServiceImp.findHouserBrowdHistoriesByUserId(userid);
        for(int i=0;i<houserBrowsingHistoryList.size();i++){
            houseInforList.add(houseInforServiceImp.findHouseInforById(houserBrowsingHistoryList.get(i).getHouseInforId()));

        }
        HashMap<Object,Object> picString=new HashMap<>();

        for(int i=0;i<houseInforList.size();i++){
            List<HousePic>    pics=housePicServiceImp.findHousePicsByHouseInforId(houseInforList.get(i).getId());
            picString.put(i,pics);
        }

        HashMap<Object,Object> map=new HashMap<>();
        map.put("houseBrowHistory",houserBrowsingHistoryList);
        map.put("HouseInfor",houseInforList);
        map.put("pic",picString);
        return new Result("200","成功",map);
    }
}
